<?php
//
// +------------------------------------------------------------------------+
// | PHP Version 5                                                          |
// +------------------------------------------------------------------------+
// | Copyright (c) All rights reserved.                                     |
// +------------------------------------------------------------------------+
// | This source file is subject to version 3.00 of the PHP License,        |
// | that is available at http://www.php.net/license/3_0.txt.               |
// | If you did not receive a copy of the PHP license and are unable to     |
// | obtain it through the world-wide-web, please send a note to            |
// | license@php.net so we can mail you a copy immediately.                 |
// +------------------------------------------------------------------------+
// | Author:                                                                |
// +------------------------------------------------------------------------+
//
// $Id$
//


/**
 * @author       mk
 */
class PdoDriver extends BaseDriver {
    
    /**
     * @var      PDO
     */
    protected $pdo;
    
    /**
     * @param    array $config    
     * @return   void
     */
    public function __construct($config) {
        
    }
    
    /**
     * @param    string $sql    
     * @return   PdoRecordSet
     */
    public function query($sql, $param = array()) {
        $sql = str_replace('{#prefix#}', $this->config['prefix'], $sql);

        $stmt = $this->pdo->prepare($sql);
        if ($stmt === false) {
            echo $sql;
            print_r($this->pdo->errorInfo());
            exit();
        }
        try {
            $stmt->execute($param);
        } catch (PDOException $e) {
            echo 'Error:'.$e->getMessage();
        }
        return new PdoRecordSet($stmt, $sql);
    }
    
    /**
     * @param    string $sql    
     * @return   int
     */
    public function exec($sql) {
        if (($ret = $this->pdo->exec($sql)) === false) {
            echo $sql;
            print_r($this->pdo->errorInfo());
            exit();
        }
        return $ret;
    }
    
    /**
     * @param    string $table    
     * @param    array $record    
     * @return   int
     */
    public function update($table, $record, $condition) {
        $str = '';
        foreach ($record as $k => $v) {
            $v = $this->quote($v);
            $str .= "`$k`=$v,";
        }
        $str = substr($str, 0, -1);
        $sql = "UPDATE `{$this->prefix}$table` SET $str WHERE $condition";
        return $this->exec($sql);
    }
    
    /**
     * @param    string $table    
     * @param    array $record    
     * @return   int
     */
    public function insert($table, $record) {
        foreach ($record as $key => $value) {
            $k_arr[] = $this->quoteIdentifierSymbol($key);
            $v_arr[] = $this->quote($value);
        }
        $key = join(',', $k_arr);
        $value = join(',', $v_arr);
        $sql = "INSERT INTO ".$this->prefix."$table($key) VALUES($value)";
        return $this->exec($sql);
    }
    
    /**
     * @param    string $table    
     * @param    array $condition    
     * @return   PdoRecordSet
     */
    public function select($table, $condition = array(), $fields = '*', $order = null, $limit = null) {
        if (!empty($conditions)) {
            $where = " WHERE";
            foreach ($conditions as $field => $value) {
                $where .= " $field = ?";
                $values[] = $value;
            }
        } else {
            $where = '';
            $values = array();
        }
        $order = empty($order) ? '' : " ORDER BY $order";
        $limit = empty($limit) ? '' : " LIMIT $limit";
        $sql = "SELECT $fields FROM `{$this->prefix}$table`$where $order $limit";
        $stmt = $this->pdo->prepare($sql);
        $stmt->execute($values);
        return new PdoRecordSet($stmt, $sql);
    }
    
    public function delete($table, $condition) {
        $sql = "DELETE FROM ".$this->prefix."$table WHERE $condition";
        return $this->exec($sql);
    }
    
    public function getLastInsertId() {
        return $this->pdo->lastInsertId();
    }
    
    public function quote($value) {
        if (is_int($value) || is_float($value)) {
            return $value;
        }
        if (is_null($value)) {
            return 'NULL';
        }
//        $value = str_replace("'", "\\'", $value);
        $value = $this->pdo->quote($value);
        return $value;
    }
    
    /**
     * 取得数据库连接
     *
     * @return PDO
     */
    public function getConnection() {
        return $this->pdo;
    }
}

